package com.zagj.videocomparess;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zagj.videocomparess.adapter.AudioAdapter;
import com.zagj.videocomparess.adapter.VideoAdapter;

/**
 * Created by Chen qin lang on 2018/6/13.
 */

public class AudioRetrieverActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, AudioAdapter.OnVideoSelectListener {

    private GridView gridview;
    private ImageView ivClose;
    private AudioAdapter videoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_select);
        initView();
        initData();
    }

    private void initData() {
        getSupportLoaderManager().initLoader(0,null,this);
    }

    private void initView() {
        ivClose = (ImageView) findViewById(R.id.iv_close);
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gridview = (GridView) findViewById(R.id.gridview_media_video);

    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String order = MediaStore.Video.Media.DATE_ADDED + " DESC";
        return new CursorLoader(this, uri, new String[]{MediaStore.Audio.Media.DATA, MediaStore.MediaColumns._ID}, null, null, order);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (data == null || data.getCount() <= 0) {
            return;
        }
        if (videoAdapter == null) {
            videoAdapter = new AudioAdapter(getApplicationContext(), data);
            //videoAdapter.setMediaSelectVideoActivity(this);
            videoAdapter.setOnSelectChangedListener(this);
        }else {
            videoAdapter.swapCursor(data);
        }
        if (gridview.getAdapter() == null) {
            gridview.setAdapter(videoAdapter);
        }
        videoAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        if (videoAdapter != null)
            videoAdapter.swapCursor(null);
    }

    @Override
    public void onSelect(String path, String cover) {
        if(!TextUtils.isEmpty(path)){
            Intent intent=new Intent(AudioRetrieverActivity.this,AudioActivity.class);
            intent.putExtra("audio_path",path);
            startActivity(intent);
            finish();
        }
    }
    @Override
    protected void onDestroy() {
        getLoaderManager().destroyLoader(0);
        Glide.get(this).clearMemory();
        super.onDestroy();
    }

}
